package com.nna88.voz.main;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.nna88.voz.contain.containEmo;
import com.nna88.voz.listview.listViewEmo;
import com.nna88.voz.util.HorizontialListView;


public class PageReply extends Activity {
	private HorizontialListView mListEmo;
	private EditText m_TextPost;
	private Button m_ButPost;
	private Button m_ButPostCancle;
	private Button m_ButPostEmo;
	private ImageButton m_ButBold;
	private ImageButton m_ButItalic;
	private ImageButton m_ButUnder;
	private ImageButton m_ButLink;
	private ImageButton m_ButImage;
	private ArrayList<containEmo> listEmo;
    private AlertDialog.Builder alert;
    private View textEntryView;
    private LayoutInflater factory;
    private EditText edit1 ;
    private EditText edit2 ;
    private TextView text1 ;
    private TextView text2 ;
    private float mTextSize;
    private Context mContext;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.pagereply);
		super.onCreate(savedInstanceState);
		mContext =this;
		mListEmo = (HorizontialListView)findViewById(R.id.Page3_emo);
		m_TextPost = (EditText)findViewById(R.id.Page3_postReply);
		m_TextPost.setFocusable(true);
		mTextSize = getIntent().getExtras().getFloat("textsize");
		m_TextPost.setTextSize(m_TextPost.getTextSize()*mTextSize);
		m_ButPost = (Button)findViewById(R.id.Page3_butReply);
		m_ButPostEmo = (Button)findViewById(R.id.Page3_butReplyEmo);
		m_ButPostCancle = (Button)findViewById(R.id.Page3_butReplyCancle);
		m_ButBold = (ImageButton)findViewById(R.id.PageReplyBold);
		m_ButItalic = (ImageButton)findViewById(R.id.PageReplyItalic);
		m_ButUnder= (ImageButton)findViewById(R.id.PageReplyUnder);
		m_ButLink= (ImageButton)findViewById(R.id.PageReplyLink);
		m_ButImage= (ImageButton)findViewById(R.id.PageReplyImage);
		m_ButBold.setOnClickListener(mBoldClick);
		m_ButItalic.setOnClickListener(mItalicClick);
		m_ButUnder.setOnClickListener(mUnderClick);
		m_ButImage.setOnClickListener(mImageClick);
		m_ButLink.setOnClickListener(mLinkClick);
		mListEmo.setOnItemClickListener(mEmoListClick);
		m_ButPost.setOnClickListener(mPostReplyClick);
		m_ButPostCancle.setOnClickListener(mPostCancleClick);
		m_ButPostEmo.setOnClickListener(mPostEmoClick);
		listEmo = new ArrayList<containEmo>();
		listEmo = Emo2();
		listViewEmo adapter2 = new listViewEmo(PageReply.this,listEmo);
	    mListEmo.setAdapter(adapter2);
		m_TextPost.setText(getIntent().getExtras().getString("msg"));
		m_TextPost.setSelection(m_TextPost.getText().length());
	}
	private ArrayList<containEmo> Emo2(){
		ArrayList<containEmo> listEmo = new ArrayList<containEmo>();
		try {
			String list[] = mContext.getAssets().list("images/smilies/Off");
			String temp;
			Bitmap emo;
			InputStream is ;
			for(int i=0;i<list.length;i=i+1){
				temp = list[i].substring(0,list[i].length()-4);
				Log.d("nna",temp);
				if(temp.equals("brick"))
					is  = mContext.getAssets().open("images/smilies/Off/"+temp+".png");
				else 
					is  = mContext.getAssets().open("images/smilies/Off/"+temp+".gif");
				emo = BitmapFactory.decodeStream(is);
				if(temp.equals("embarrassed")) 				temp = ":\">";
				else if(temp.equals("nosebleed"))			temp = "chaymau";
				else if(temp.equals("feel_good"))			temp = "sogood";
				else if(temp.equals("sexy_girl"))			temp = "sexy";
				else if(temp.equals("look_down"))			temp = "look_down";
				else if(temp.equals("beat_brick"))			temp = "brick";
				else if(temp.equals("cry"))					temp = ":((";
				else if(temp.equals("beat_plaster"))		temp = ":-s";
				else if(temp.equals("confuse"))				temp = "chaymau";
				else if(temp.equals("big_smile"))			temp = ":D";
				else if(temp.equals("brick"))				temp = "gach";
				else if(temp.equals("burn_joss_stick"))		temp = "stick";
				else if(temp.equals("bad_smell"))			temp = "badsmell";
				else if(temp.equals("cool"))				temp = "kool";
				else if(temp.equals("after_boom"))			temp = "aboom";
				else if(temp.equals("beat_shot"))			temp = "shot";
				else if(temp.equals("sweet_kiss"))			temp = ":*";
				else if(temp.equals("beat_shot"))			temp = "shot";
				else if(temp.equals("smile"))				temp = ":)";
				else if(temp.equals("still_dreaming"))		temp = "dreaming";
				else if(temp.equals("beat_shot"))			temp = "shot";
				else if(temp.equals("beat_shot"))			temp = "shot";
				listEmo.add(new containEmo(emo, ":"+temp+":"));
				is.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listEmo;
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();

	}
	private void setPostVisible(int type) {
		m_ButPost.setVisibility(type);
		m_ButPostCancle.setVisibility(type);
		m_ButPostEmo.setVisibility(type);
		m_TextPost.setVisibility(type);
		
	}
	public final Handler mHandler = new Handler(){
    	@Override
    	public void handleMessage(Message msg) {
    		 switch (msg.what) {
    		 case 20:
    			int start =m_TextPost.getSelectionStart();
 				m_TextPost.getText().insert(start,(String) msg.obj);
     			break;	
    		 }
    	};
	};
	OnItemClickListener mEmoListClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			mHandler.obtainMessage(20,listEmo.get(arg2).text()).sendToTarget();
			
		}
		
	};
	OnClickListener mPostReplyClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			setPostVisible(View.GONE);
			mListEmo.setVisibility(View.GONE);
			Intent i = new Intent();
			Bundle extras = new Bundle();
 			extras.putString("msg",m_TextPost.getText().toString());
 			i.putExtras(extras);
			setResult(RESULT_OK,i);
			finish();
		}
	};
	OnClickListener mPostCancleClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			setResult(RESULT_CANCELED);
            finish();
		}
	};
	OnClickListener mImageClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			alertDialog();
			alertInsert();
			alert.setTitle("Please enter the url of your link");
			alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
		    	public void onClick(DialogInterface dialog, int whichButton) {
		    		int start =m_TextPost.getSelectionStart();
		    		String temp = "[IMG]"+edit1.getText().toString()+"[/IMG]";
					m_TextPost.getText().insert(start,temp);
		    	}
		    });
			alert.setNegativeButton("CANCLE", new DialogInterface.OnClickListener() {
		    	public void onClick(DialogInterface dialog, int whichButton) {
		    	}
		    });
			alert.show();
		}
	};
	OnClickListener mLinkClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			alertDialog();
			alertInsert();
			alert.setTitle("Please enter the url of your link");
			alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
		    	public void onClick(DialogInterface dialog, int whichButton) {
		    		int start =m_TextPost.getSelectionStart();
		    		String temp = "[URL=\""+edit1.getText().toString()+"\"]"+
		    				edit1.getText().toString()+"[/URL]";
					m_TextPost.getText().insert(start,temp);
		    	}
		    });
			alert.setNegativeButton("CANCLE", new DialogInterface.OnClickListener() {
		    	public void onClick(DialogInterface dialog, int whichButton) {
		    	}
		    });
			alert.show();
		}
	};
	OnClickListener mBoldClick = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int start =m_TextPost.getSelectionStart();
				m_TextPost.getText().insert(start, "[B][/B]");
			}
		};
	OnClickListener mItalicClick = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int start =m_TextPost.getSelectionStart();
				m_TextPost.getText().insert(start, "[I][/I]");
	
			}
		};
	OnClickListener mUnderClick = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int start =m_TextPost.getSelectionStart();
				m_TextPost.getText().insert(start, "[U][/U]");
	
			}
		};
	OnClickListener mPostEmoClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(mListEmo.getVisibility() != View.VISIBLE){ 
				mListEmo.setVisibility(View.VISIBLE);
			}else mListEmo.setVisibility(View.GONE);
		}
	};
	private void alertDialog(){
	    factory = LayoutInflater.from(PageReply.this);
	    textEntryView = factory.inflate(R.layout.login, null);    
	    edit1 = (EditText)textEntryView.findViewById(R.id.alert_edit1);
	    edit2 = (EditText)textEntryView.findViewById(R.id.alert_edit2);
	    text1 = (TextView)textEntryView.findViewById(R.id.alert_txt1);
	    text2 = (TextView)textEntryView.findViewById(R.id.alert_txt2);
	    alert = new AlertDialog.Builder(PageReply.this);
	    alert.setView(textEntryView);
    }
	 private void alertInsert(){
	    	edit2.setVisibility(View.GONE);
	    	text2.setVisibility(View.GONE);
	    	text1.setVisibility(View.GONE);
	    	edit1.setText("http://");
	    	edit1.setSelection(edit1.getText().length());
	    }
}
